package com.scenic.rd_android.memory;

import android.animation.ObjectAnimator;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.widget.Button;
import android.widget.Toast;

import com.nova.scenic.projectlibs.util.BaseActivity;
import com.nova.scenic.projectlibs.util.MemoryUtil;
import com.nova.scenic.projectlibs.util.debug.MyLog;
import com.nova.scenic.projectlibs.util.lifecyclehelper.LifeCycleHelper;
import com.nova.scenic.projectlibs.util.lifecyclehelper.LifeCycleHelperBase;
import com.scenic.rd_android.R;

public class MemoryLack1Activity extends BaseActivity {

    private static final String TAG = "MemoryLack1Activity";

    {
        setLifeCycleHelper(new MemoryLakeByAnimation(getLifeCycleHelper()));
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    /**
     * 属性动画导致的内存泄露
     */
    class MemoryLakeByAnimation extends LifeCycleHelperBase {

        public MemoryLakeByAnimation(LifeCycleHelper helper) {
            super(helper);
        }

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_memory);
            Button button = (Button) findViewById(R.id.id_button_memoryTest);
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) {

                ObjectAnimator animator = ObjectAnimator.ofFloat(button, "", 0, 180);
                animator.setDuration(2000);
                animator.setRepeatCount(-1);
                animator.start(); // 没有调用cancle()
                finish();
            } else {
                Toast.makeText(getApplicationContext(), "os version not support", Toast.LENGTH_LONG).show();
            }

        }
    }

    class TestMemoryLake2 extends LifeCycleHelperBase {

        //http://www.jianshu.com/p/8c6cf3d7a98a
//        Shallow heap表示当前对象所消耗的内存
//        Retained heap表示当前对象所消耗的内存加上它引用的内存总合
        public TestMemoryLake2(LifeCycleHelper helper) {
            super(helper);
        }

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            // Post a message and delay its execution for 10 minutes.
            mLeakyHandler.postDelayed(new Runnable() {
                @Override
                public void run() { /* ... */ }
            }, 1000 * 60 * 10);

            MemoryLack1Activity.this.finish();
        }


        private final Handler mLeakyHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                // ...
            }
        };

    }

    class TestMemoryLake1 extends LifeCycleHelperBase {

        public TestMemoryLake1(LifeCycleHelper helper) {
            super(helper);
        }

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            int heapSize = new MemoryUtil().getHeapSize(MemoryLack1Activity.this);
            MyLog.i(TAG, "heapSize is " + heapSize);

            MyLog.i(TAG, "Looper instance is " + MyLog.getInstanceName(Looper.getMainLooper()));

            handler.sendEmptyMessage(0);
        }


        Handler handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        while (true) {
                            try {
                                Thread.sleep(1000);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                            MyLog.i(TAG, "instance " + MyLog.getInstanceName(MemoryLack1Activity.this) + " execute handle message");
                        }
                    }
                }).start();

            }
        };

    }

}
